<?php

class Public_LoginAction extends BlogPublicBaseAction
{
    /**
     * Process HTTP GET method. If user is not logged in display error 404.
     *
     * @param AgaviRequestDataHolder $rd
     */
    /*
    public function executeRead(AgaviRequestDataHolder $rd)
    {
        return array('Public', 'Error404Success');
    }
	*/
    
    /**
     * Process POST method
     *
     * @param AgaviRequestDataHolder $rd
     * @return mixed
     */
    public function executeWrite(AgaviRequestDataHolder $rd)
    {
        $params = $rd->getParameters();
        
        $this->setLoginNameCookie($params['username']);
        
        try {
           $this->getContext()->getUser()->login($params['username'], $params['password']);
           return 'Success';
        } catch(AgaviSecurityException $e) {
           $this->setAttribute('error', $e->getMessage());
           return 'Error';
        }
    }
    
    public function handleError(AgaviRequestDataHolder $rd)
    {
        $params = $rd->getParameters();
        $this->setLoginNameCookie($params['username']);
        
        return 'Error';
    }
    
    
	/**
	 * Returns the default view if the action does not serve the request
	 * method used.
	 *
	 * @return     mixed <ul>
	 *                     <li>A string containing the view name associated
	 *                     with this action; or</li>
	 *                     <li>An array with two indices: the parent module
	 *                     of the view to be executed and the view to be
	 *                     executed.</li>
	 *                   </ul>
	 */
	public function getDefaultViewName()
	{
		return 'Success';
	}
	

	
	protected function setLoginNameCookie($username)
	{
        $ro = $this->getContext()->getRouting();
        /* @var $response AgaviWebResponse */
        $response = $this->getContext()->getController()->getGlobalResponse();
        $response->setCookie('login_username', $username, 60*60*24*365, $ro->gen('root'));
	}
}

?>